import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import PreferenceUtil from '../common/utils/PreferenceUtil';

export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    // 1.加载用户首选项
    PreferenceUtil.init(this.context)
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // // 1.获取应用主窗口。
    // let mainWindow = windowStage.getMainWindowSync();
    // // 2.实现沉浸式效果：设置导航栏、状态栏不显示。
    // mainWindow.setWindowSystemBarEnable(["status"]);
    // hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    // 3.为沉浸式窗口加载对应的目标页面。
    windowStage.loadContent('pages/WelcomePage', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
    // // 4.设置窗口为全屏显示
    // mainWindow.setWindowLayoutFullScreen(true)
    // // 5.获取状态栏高度
    // let statusBarHeight = mainWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT).topRect.height
    // // 6.将状态栏高度保存起来
    // PersistentStorage.PersistProp('statusBarHeight', statusBarHeight)
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
